package com.eorchis.module.classdept.ui.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import com.eorchis.core.service.IBaseService;
import com.eorchis.core.ui.commond.ResultState;
import com.eorchis.core.ui.commond.converter.IQueryCommondAttributeConverter;
import com.eorchis.core.ui.controller.AbstractBaseController;
import com.eorchis.module.classdept.domain.ClassDeptQueryBean;
import com.eorchis.module.classdept.service.IClassDeptService;
import com.eorchis.module.classdept.ui.commond.ClassDeptQueryCommond;
import com.eorchis.module.classdept.ui.commond.ClassDeptValidCommond;
import com.eorchis.module.preauthorizationregistration.domain.PreSqSyUserBean;

/**
 * Title: ClassDeptController<br>
 * Description: 班级部门关系表管理控制器<br>
 * Company: GoldGov<br>
 * Copyright @ 2012 GoldGov .All rights reserved.<br>
 * @author Max
 * @createDate 2014-12-30
 * @version $Revision$
 */
@Controller("classDeptController")
@RequestMapping(value = ClassDeptController.MODULE_PATH)
public class ClassDeptController 
		extends AbstractBaseController<ClassDeptValidCommond,ClassDeptQueryCommond> 
{
	// 模块页面根路径
	public static final String MODULE_PATH = "/module/classdept";
	// 班级部门关系表接口
	@Autowired
	@Qualifier("com.eorchis.module.classdept.service.impl.ClassDeptServiceImpl")
	private IClassDeptService classDeptService;
	
	public IBaseService getService() {
		return classDeptService;
	}

	public String getModulePath() {
		return MODULE_PATH;
	}
	@Autowired
	@Qualifier("attributeConverter")
	private ArrayList<IQueryCommondAttributeConverter> attributeConverter;
	
	/**
	 * 查询未选单位列表
	 * @param commond
	 * @return
	 * @throws Exception
	 * @author Max
	 * @createDate 2014-12-31
	 */
	@RequestMapping(value = "/findNoSeleDeptList")
	public String findNoSeleDeptList(
			@ModelAttribute("result") ClassDeptQueryCommond qCommond,
			HttpServletRequest request, HttpServletResponse response,
			@ModelAttribute("resultState") ResultState resultState
			) throws Exception {
		
		if (attributeConverter != null) {
			parameterValueConvert(qCommond, request, attributeConverter);
		}
		List<ClassDeptQueryBean> list=classDeptService.findNoSeleDeptList(qCommond);
		qCommond.setResultList(list);
		resultState.setState(ResultState.RESULT_STATE_SUCCESS);
		return "";
	}
	
	/**
	 * 查询已选单位列表
	 * @param commond
	 * @return
	 * @throws Exception
	 * @author Max
	 * @createDate 2014-12-31
	 */
	@RequestMapping(value = "/findSeleDeptList")
	public String findSeleDeptList(
			@ModelAttribute("result") ClassDeptQueryCommond qCommond,
			HttpServletRequest request, HttpServletResponse response,
			@ModelAttribute("resultState") ResultState resultState
			) throws Exception {
		
		if (attributeConverter != null) {
			parameterValueConvert(qCommond, request, attributeConverter);
		}
		List<ClassDeptQueryBean> list=classDeptService.findSeleDeptList(qCommond);
		qCommond.setResultList(list);
		resultState.setState(ResultState.RESULT_STATE_SUCCESS);
		return "";
	}
	
	
	/**
	 * 给班级添加部门
	 * @param commond
	 * @return
	 * @throws Exception
	 * @author Max
	 * @createDate 2014-12-30
	 */
	@RequestMapping(value = "/addDeptForClass")
	public String addDeptForClass(
			@ModelAttribute("result") ClassDeptValidCommond vCommond,
			HttpServletRequest request, HttpServletResponse response,
			@ModelAttribute("resultState") ResultState resultState
			) throws Exception {
		Boolean state=classDeptService.addDeptForClass(vCommond);
		if(state) {
			resultState.setMessage("添加成功");
			resultState.setState(ResultState.RESULT_STATE_SUCCESS);
		}else {
			resultState.setMessage("添加失败");
			resultState.setState(ResultState.RESULT_STATE_FAIL);
		}
		return "";
	}
	
	/**
	 * 删除部门单位关系
	 * @param commond
	 * @return
	 * @throws Exception
	 * @author Max
	 * @createDate 2014-12-31
	 */
	@RequestMapping(value = "/deleteClassDept")
	public String deleteClassDept(
			@ModelAttribute("result") ClassDeptValidCommond  vCommond,
			HttpServletRequest request, HttpServletResponse response,
			@ModelAttribute("resultState") ResultState resultState
			) throws Exception {
		
		if(vCommond.getSearchClassDepIds()!=null&&vCommond.getSearchClassDepIds().length>0) {
			String value=classDeptService.deleteClassDept(vCommond);
			if(value.equals("")) {
				resultState.setMessage("删除成功");
				resultState.setState(ResultState.RESULT_STATE_SUCCESS);
			}else {
				resultState.setMessage(value);
				resultState.setState(ResultState.RESULT_STATE_FAIL);
			}
		}else {
			resultState.setMessage("未传入删除对象");
			resultState.setState(ResultState.RESULT_STATE_FAIL);
		}
		return "";
	}
	
	/**
	 * 删除部门单位关系
	 * @param commond
	 * @return
	 * @throws Exception
	 * @author Max
	 * @createDate 2014-12-31
	 */
	@RequestMapping(value = "/delDeptByClassId")
	public String delDeptByClassId(
			@ModelAttribute("result") ClassDeptValidCommond  vCommond,
			HttpServletRequest request, HttpServletResponse response,
			@ModelAttribute("resultState") ResultState resultState
			) throws Exception {
		
		if(vCommond.getSearchClassId()!=null) {
			classDeptService.delDeptByClassId(vCommond);
			resultState.setMessage("删除成功");
			resultState.setState(ResultState.RESULT_STATE_SUCCESS);
		}else {
			resultState.setMessage("未传入删除对象");
			resultState.setState(ResultState.RESULT_STATE_FAIL);
		}
		return "";
	}
	
	
	/**
	 * 修改部门人数
	 * @param commond
	 * @return
	 * @throws Exception
	 * @author Max
	 * @createDate 2015-1-7
	 */
	@RequestMapping(value = "/updateDeptUserNum")
	public String updateDeptUserNum(
			@ModelAttribute("result") ClassDeptValidCommond  vCommond,
			HttpServletRequest request, HttpServletResponse response,
			@ModelAttribute("resultState") ResultState resultState
			) throws Exception {
		/*
		 * 1.如果单位没有单位参学名额 ，不能去修改人数
		 * 2.修改人数最大限制不能大于单位的参学名额
		 */
		String value=classDeptService.updateDeptUserNum(vCommond);
		if(value.equals("")) {
			resultState.setMessage("修改成功");
			resultState.setState(ResultState.RESULT_STATE_SUCCESS);
		}else {
			resultState.setMessage(value);
			resultState.setState(ResultState.RESULT_STATE_FAIL);
			
		}
		return "";
	}
	
	
	/**
	 * 分配参学名额的列表分配人数汇总
	 * @param querycommond
	 * @param request
	 * @param response
	 * @param resultState
	 * @return
	 * @throws Exception
	 * @author Max
	 * @createDate 2015-01-23
	 */
	@RequestMapping(value = "/findSeleDeptNumList")
	public String findSeleDeptNumList(
			@ModelAttribute("resultList") ClassDeptQueryCommond queryCommond,
			HttpServletRequest request, HttpServletResponse response,
			@ModelAttribute("resultState") ResultState resultState,Model model) throws Exception {
		Integer num=classDeptService.findSeleDeptNumList(queryCommond);
		PreSqSyUserBean pb=new PreSqSyUserBean();
		pb.setSqUserNum(num);
		model.addAttribute("result", pb);
		resultState.setMessage("操作成功！");
		resultState.setState(ResultState.RESULT_STATE_SUCCESS);
		
		return "";
	}
	
	
	
	/**
	 * 用于排序的方法
	 * @param queryCommond
	 * @param request
	 * @param attributeConverter
	 */
	@SuppressWarnings("unchecked")
	private void parameterValueConvert(ClassDeptQueryCommond queryCommond,
			HttpServletRequest request,
			List<IQueryCommondAttributeConverter> attributeConverter) {
		for (IQueryCommondAttributeConverter queryCommondAttributeConverter : attributeConverter) {
			queryCommondAttributeConverter.convert(queryCommond,
					request.getParameterMap());
		}
	}
}
